//
//  MOWorkTaskSelectdTaskTypeView.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/12.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOWorkTaskSelectdTaskTypeView.h"
#import "MOWorkTaskSelectdTaskTypeCell.h"

@interface MOWorkTaskSelectdTaskTypeView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIView *backView;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,assign)NSInteger selectedIndex;

@property(nonatomic,strong)UIView *showView;

@end

@implementation MOWorkTaskSelectdTaskTypeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self) {
        
        self.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.01];
     
        [self addSubview:self.showView];
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [button addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        [self addSubview:self.backView];
        [UIView animateWithDuration:0.3 animations:^{
            self.backView.y = StatusBarAndNavigationBarHeight + 44;
        }];
        
    }
    return self;
}


-(void)setResultArray:(NSMutableArray *)resultArray
{
    self.dataArray = [NSMutableArray array];
    for (int i = 0; i<resultArray.count; i++) {
        MOWorkTaskSelectdTaskTypeModel *model = resultArray[i];
        if ([self.currentIdStr integerValue]>0) {
            if ([model.idStr integerValue]==[self.currentIdStr integerValue]) {
                model.selectedStatus = @"1";
                self.selectedIndex = i;
            }
            else{
                model.selectedStatus = @"0";
            }
        }else{
            model.selectedStatus = @"0";
        }
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];
    
    NSInteger r = [self.tableView numberOfRowsInSection:0]; //最后一组有多少行
    if (r<5||self.selectedIndex<4) return;
    NSIndexPath *ip = [NSIndexPath indexPathForRow:self.selectedIndex inSection:0];  //取最后一行数据
    [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:YES]; //滚动到最后一行
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWorkTaskSelectdTaskTypeCell *cell = [MOWorkTaskSelectdTaskTypeCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 46;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndex = indexPath.row;
//    for (int i = 0; i<self.dataArray.count; i++) {
//        MOWorkTaskSelectdTaskTypeModel *model = self.dataArray[i];
//        if (i==indexPath.row) {
//            model.selectedStatus = @"1";
//        }else{
//            model.selectedStatus = @"0";
//        }
//    }
//    [self.tableView reloadData];
    
    [self clickCancelButton];
    
    MOWorkTaskSelectdTaskTypeModel *model = self.dataArray[indexPath.row];
    model.selectedStatus = @"1";
    if (self.clickSaveButtonBlock) {
        self.clickSaveButtonBlock(model);
    }
}


#pragma mark -- 取消
-(void)clickCancelButton
{
    if (self.clickCancelButtonBlock) {
        self.clickCancelButtonBlock();
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.y = -self.backView.height;
    } completion:^(BOOL finished) {
        self.frame = CGRectMake(0, 0, 0, 0);
        self.backView.height = 0;
        self.showView.height = 0;
    }];
}


#pragma mark -- 懒加载
-(UIView *)backView
{
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, -4*46, SCREEN_WIDTH, 4*46)];
        _backView.backgroundColor = KWhiteColor;
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _backView.height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = KWhiteColor;
        [_backView addSubview:_tableView];
        
    }
    return _backView;
}

-(UIView *)showView
{
    if (!_showView) {   
        _showView = [[UIView alloc]initWithFrame:CGRectMake(0, StatusBarAndNavigationBarHeight + 44, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _showView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.57];
    }
    return _showView;
}


@end
